package com.helper.dream.a361phonehelper.utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by Administrator on 2016/12/21.
 */

public class DownString {
    public static void loadString(final String url, final CallBack cb){
        String msg = null;
        L.d("url:---" + url);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .build();
        okhttp3.Call call = client.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                L.d("abc" ,"失败~~~~~~~~~" + url);
            }

            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                String msg = response.body().string();
                try {
                    L.d("abc", url);
                    JSONObject jo = new JSONObject(msg);
                    int errno = jo.getInt("errno");
                    if(errno == 0){
                        cb.htmlString(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public static void loadString2(final String url, final CallBack2 cb){
        String msg = null;
        L.d("url:---" + url);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .build();
        okhttp3.Call call = client.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                cb.fail();
            }

            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                String msg = response.body().string();
               cb.success(msg);
            }
        });
    }
    public interface CallBack{
        void htmlString(String msg);

    }
    public interface CallBack2{
        void success(String msg);
        void fail();
    }
}
